<template>
	<view class="search">
		<view class="local">
			<picker mode="region" @change="change">
				<view>{{address}}</view>
			</picker>
			<uv-icon name="arrow-down-fill" size="22rpx"></uv-icon>
		</view>
		<view class="input">
			<input v-model="keyword" class="placeHolder" placeholder="搜索关键字" />
		</view>
		<view class="btn" @click="search">搜索</view>
	</view>
</template>
<script>
	import {
		LatLntGetLocation
	} from '@/API/articleNew.js'
	export default {
		data() {
			return {
				keyword: '',
				area: '',
				address: ''
			}
		},
		mounted() {
			this.fetchUserLocation(); 
		},
		methods: {
			search() {
				this.$emit('search', this.keyword);
			},
			change(e) {
				const locationData = e.detail.value;
				this.area = `${locationData[0]}/${locationData[1]}/${locationData[2]}`;
				this.address = locationData[2];
				this.$emit('region-change', this.area);
			},
			fetchUserLocation() {
				uni.getLocation({
					type: 'wgs84',
					success: (res) => {
						const {
							latitude,
							longitude
						} = res;
						console.log(res, "8787878");
						this.reverseGeocode(latitude, longitude);
					}
				});
			},
			reverseGeocode(latitude, longitude) {
				LatLntGetLocation({
					'lng': longitude,
					'lat': latitude
				}).then(res => {
					
					const locationData = res;
					console.log(locationData, '989898989');
					this.area = `${locationData.province}/${locationData.city}/${locationData.district}`;
					this.address = locationData.district;
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.search {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 5rpx 15rpx;
		background-color: white;
		border-radius: 50rpx;
		gap: 40rpx;
		font-size: 26rpx;

		.local {
			display: flex;
			align-items: center;
			gap: 10rpx;
			font-weight: 500;
			color: #121212;
			position: relative;

			&::after {
				content: '';
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				right: -20rpx;
				width: 1rpx;
				height: 20rpx;
				background-color: #121212;
			}
		}

		.input {
			flex: 1;
			display: flex;
			align-items: center;
			height: 60rpx;

			.placeHolder {
				font-weight: 400;
				font-size: 24rpx;
				color: #999999;
			}
		}

		.btn {
			width: 115rpx;
			height: 54rpx;
			background: linear-gradient(90deg, #DD551B, #F49355);
			border-radius: 27rpx;
			font-size: 26rpx;
			color: #FFFFFF;
			line-height: 54rpx;
			text-align: center;
			margin-left: auto;
		}
	}
</style>